import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/views/default_views.dart';

import '../entity/asset_general_detail_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/dialog_manager.dart';

///
/// 股票流水，原始股或流通股
///
class AssetYuanshiguDetail extends StatefulWidget {
  String pageTitle;
  String assetType;
  String amount;
  String amount2;

  AssetYuanshiguDetail(
      {Key? key, this.pageTitle = "", this.assetType = '', this.amount = '0', this.amount2 = '0'})
      : super(key: key);

  @override
  State<AssetYuanshiguDetail> createState() => _AssetYuanshiguDetailState();
}

class _AssetYuanshiguDetailState extends State<AssetYuanshiguDetail> {
  var page = 1;
  List<AssetGeneralDetailDataData> datas = [];
  var subTitle = '';

  @override
  void initState() {
    super.initState();
    subTitle = widget.pageTitle;
    getDatas();
  }

  getDatas() async {
    dynamic result = await Api.getassetDetail(
        {'page': page.toString(), 'limit': '20', 'type': widget.assetType});
    AssetGeneralDetailEntity entity = AssetGeneralDetailEntity.fromJson(result);

    setState(() {
      if (page == 1) {
        datas.clear();
      }

      if (entity.data.data.isNotEmpty) {
        page++;
        datas.addAll(entity.data.data);
      }
    });
  }

  String getAssetIcon() {
    if (widget.assetType.contains('stock_num') || widget.assetType.contains('floating_stock')) {
      return 'assets/images/icon_mine_stock.jpg';
    }
    return 'assets/images/icon_mine_stock.png';
  }

  Color getMoneyColor(String money) {
    if (money.startsWith('-')) {
      return Colors.red;
    } else {
      return Config.colorMain;
    }
  }

  Widget getListItem(int index) {
    return Container(
      height: 134.w,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: 3.w, color: Config.colorDivider)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                datas[index].memo,
                style: TextStyle(
                  fontSize: 27.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(padding: EdgeInsets.only(top: 8.w)),
              Text(
                datas[index].createdAt,
                style: TextStyle(
                  fontSize: 25.w,
                  color: Config.colorText3,
                ),
              ),
            ],
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                datas[index].money,
                style: TextStyle(
                  fontSize: 25.w,
                  color: getMoneyColor(datas[index].money),
                ),
              ),
              // Padding(padding: EdgeInsets.only(top: 8.w)),
              // Text(
              //   datas[index].ext,
              //   style: TextStyle(
              //     fontSize: 24.w,
              //     color: Config.colorText1,
              //   ),
              // ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: Text(widget.pageTitle),
          centerTitle: true,
        ),
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              Container(
                width: double.infinity,
                height: 200.w,
                padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 25.w),
                margin: EdgeInsets.fromLTRB(30.w, 30.w, 30.w, 0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          getAssetIcon(),
                          width: 30.w,
                          height: 30.w,
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 15.w),
                        ),
                        Text(
                          subTitle,
                          style: TextStyle(fontSize: 27.sp, color: Config.colorText1),
                        ),
                      ],
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 8.w),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text(
                          widget.amount,
                          style: TextStyle(
                              fontSize: 30.sp,
                              color: Config.colorText1,
                              fontWeight: FontWeight.bold),
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 5.w),
                        ),
                        Text(
                          widget.amount2,
                          style: TextStyle(fontSize: 26.sp, color: Config.colorText1),
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 5.w),
                        ),
                        Visibility(
                          visible: widget.pageTitle.contains('原始股'),
                          child: Container(
                            margin: EdgeInsets.only(top: 15.w),
                            width: 140.w,
                            height: 44.w,
                            child: ElevatedButton(
                              clipBehavior: Clip.antiAlias,
                              onPressed: () {
                                Routes.navigateTo(context, Routes.mineStockExchange);
                              },
                              style: ButtonStyle(
                                elevation: MaterialStateProperty.all(0),
                                backgroundColor: MaterialStateProperty.all(Colors.white),
                                shape: MaterialStateProperty.all(
                                  RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(24.w),
                                  ),
                                ),
                                side: MaterialStateProperty.all(
                                  BorderSide(color: Config.colorMain, width: 2.w),
                                ),
                              ),
                              child: Text(
                                '前往兑换',
                                style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                              ),
                            ),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              Container(
                  width: double.infinity,
                  height: double.infinity,
                  padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                  margin: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 30.w, top: 260.w),
                  decoration: BoxDecoration(
                    color: (Config.colorWhite),
                    borderRadius: BorderRadius.circular(12.w),
                  ),
                  child: datas.isNotEmpty
                      ? EasyRefresh(
                          header: const MaterialHeader(),
                          footer: const MaterialFooter(),
                          onRefresh: () async {
                            await Future.delayed(const Duration(milliseconds: 200), () {
                              page = 1;
                              getDatas();
                            });
                          },
                          onLoad: () async {
                            getDatas();
                          },
                          child: ListView.builder(
                            // physics: const NeverScrollableScrollPhysics(),
                            itemCount: datas.length,
                            padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
                            itemBuilder: (BuildContext context, int index) {
                              return getListItem(index);
                            },
                          ),
                        )
                      : showNullData()),
            ],
          ),
        ));
  }
}
